package ship.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import ship.service.shipservice;

/**
 * Servlet implementation class loginbystaffServlet
 */
@WebServlet("/loginbystaffServlet")
public class loginbystaffServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public loginbystaffServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		int no = Integer.parseInt(request.getParameter("sno"));
        String nm = request.getParameter("snm");
        //调用业务方法判断用户是否登录成功
        shipservice service = new shipservice();
        boolean result = service.Loginbystafftablesnoandsnm(no, nm);
		/*
		 * response.setContentType("text/html; charset=utf-8");
		 * response.setCharacterEncoding("utf-8"); PrintWriter out =
		 * response.getWriter();
		 */
        if(result){ 
        	HttpSession session = request.getSession();
     	    session.setAttribute("sno", no);
            session.setAttribute("snm", nm);
            session.setMaxInactiveInterval(60*60);
			/* out.println("登录成功!!"); */
			request.getRequestDispatcher("index1/index1_1_1.jsp").forward(request, response);
        }
        else{
            String mess = "您的代号编码或您的姓名有误,请重新尝试!";
            request.setAttribute("mess", mess); 
            request.getRequestDispatcher("loginbystaff.jsp").forward(request, response);
        }
	}

}
